package com.vcg.community.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import vc.dubbo.result.DubboResult;

import com.vcg.community.dao.RatelogMapper;
import com.vcg.community.dao.ResourceMapper;
import com.vcg.community.dao.UserMapper;
import com.vcg.community.model.Ratelog;
import com.vcg.community.model.Resource;
import com.vcg.community.model.User;
import com.vcg.community.model.Userrelation;

public class VoteServiceImpl implements VoteService {
	@Autowired
	ResourceMapper rsDao;
	@Autowired
	UserMapper userDao;
	@Autowired
	RatelogMapper rlDao;	
	@Autowired
	DataSourceTransactionManager txManager;

	public DubboResult<List<Resource>> getVotedResources(Integer userId) {
		DubboResult<List<Resource>> resultObject=new DubboResult<List<Resource>>(200,null);
		try{
			List<Resource> lstResource=rsDao.getVotedResources(userId);
			for(int i=0;i<lstResource.size();i++){
				Resource rs=lstResource.get(i);
				rs.setUrl("http://photo-community.shijue.me/"+rs.getResourceId()+".jpg");
			}
			resultObject.setResult(lstResource);
		}catch(Exception ex){
			resultObject.setCode(521);
			resultObject.setCodeError(ex.toString());
			
		}
		return resultObject;
	}
	public DubboResult voteForResource(Integer userId,Integer resourceId){
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

		TransactionStatus status = txManager.getTransaction(def);

		DubboResult resultObject=new DubboResult(200,"",null);
		try{
			Ratelog rlObject=new Ratelog();
			rlObject.setUserId(userId);
			rlObject.setTargetId(resourceId);
			rlObject.setCreatedtime(new Date());
			rlDao.insert(rlObject);
		}catch(Exception ex){
			txManager.rollback(status);
			resultObject.setCode(521);
			resultObject.setCodeError(ex.toString());
			return resultObject;
		}
		txManager.commit(status);
		return resultObject;		
	}
	public DubboResult<List<User>> getVotedUsers(Integer resourceId) {
		DubboResult<List<User>> resultObject=new DubboResult<List<User>>(200,null);
		try{
			List<User> lstUser=userDao.getVotedUsersByResourceId(resourceId);
			resultObject.setResult(lstUser);
		}catch(Exception ex){
			resultObject.setCode(521);
			resultObject.setCodeError(ex.toString());
			
		}
		return resultObject;
	}
	public DubboResult<List<Resource>> getVotedResources(String strUserId) {
		// TODO Auto-generated method stub
		return getVotedResources(getUserbyStringId(strUserId));
	}
	public DubboResult voteForResource(String strUserId, String strResourceId) {
		// TODO Auto-generated method stub
		return voteForResource(getUserbyStringId(strUserId),getResourcebyStringId(strResourceId));
	}
	public DubboResult<List<User>> getVotedUsers(String strResourceId) {
		// TODO Auto-generated method stub
		return getVotedUsers(getResourcebyStringId(strResourceId));
	}
	private Integer getUserbyStringId(String strUserId){
		User usr=userDao.SelectUserByStringId(strUserId);
		if(usr!=null){
		return usr.getId();
		}else{
			return -1;
		}
	}
	private Integer getResourcebyStringId(String strResourceId){
		Resource rs=rsDao.selectByResourceID(strResourceId);
		if(rs!=null){
			return rs.getId();
		}else{
			return -1;
		}
	}

}
